<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class NationSendGift extends Base
{
    protected $table = 'nation_send_gift_log';
    protected $user_collect_rank = 'NATION:USER:COLLECT:GIFT'; //收
    protected $user_send_rank = 'NATION:USER:SEND:GIFT';//送
    protected $room_rank = 'NATION:ROOM:RANK'; //房间

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //查看用户记录
    public function getLogByUserId($user_id)
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->first();
        return $this->objToArray($res);
    }

    public function createLog($user_id, $veil_num, $kiss_num)
    {
        $data['user_id'] = $user_id;
        $data['veil_num'] = $veil_num;
        $data['kiss_num'] = $kiss_num;
        $data['is_ring'] = 0;
        $data['status'] = 0;
        return DB::table($this->table)->insertGetId($data);
    }

    public function updateLog($user_id, $veil_num, $kiss_num)
    {
        $where['user_id'] = $user_id;
        if ($veil_num == 0 && $kiss_num == 0) {
            return false;
        }
        if ($veil_num != 0) {
            DB::table($this->table)->where($where)->increment('veil_num', $veil_num);
        }
        if ($kiss_num != 0) {
            DB::table($this->table)->where($where)->increment('kiss_num', $kiss_num);
        }
        return true;
    }

    public function updateRing($user_id)
    {
        $where['user_id'] = $user_id;
        $where['status'] = 0;
        $log = DB::table($this->table)->where($where)->first();
        if(!$log){
            return true;
        }
        $log = $this->objToArray($log);
        if($log['veil_num'] > 9 && $log['kiss_num']>0){
            //更新
            return DB::table($this->table)->where($where)->update(['status'=>1]);
        }
        return true;
    }


    public function rankCache($user_id,$to_user_ids,$room_id,$send_sorce,$collect_sorce)
    {
        $user_collect_rank = $this->user_collect_rank; //收
        $user_send_rank = $this->user_send_rank;//送
        $room_rank = $this->room_rank;//房间
        foreach($to_user_ids as $v){
            Redis::zincrby($user_collect_rank,$collect_sorce , $v);
        }
        Redis::zincrby($user_send_rank,$send_sorce , $user_id);
        if($room_id > 0 ){
            Redis::zincrby($room_rank,$send_sorce , $room_id);
        }
        return true;
    }

    public function clickRing($user_id)
    {
        $where['user_id'] = $user_id;
        $where['status'] = 1;
        return DB::table($this->table)->where($where)->update(['is_ring'=>1]);
    }



    //用户总榜
    public function allSendRank()
    {
        return Redis::zrevrange($this->user_send_rank, 0, 4);
    }

    //用户收总榜
    public function allCollectRank()
    {
        return Redis::zrevrange($this->user_collect_rank, 0, 4);
    }

    //房间收总榜
    public function allRoomRank()
    {
        return Redis::zrevrange($this->room_rank, 0, 2);
    }




}